package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.ConstellationBean
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.view.TypeFaceTextView

class CompatibilityAdapter(private val mContext: Context) : BaseAdapter() {

    private val mDataList = ArrayList<ConstellationBean>()

    fun setData(list: ArrayList<ConstellationBean>) {
        mDataList.clear()
        mDataList.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext)
            .inflate(R.layout.item_compatibility, parent, false)
        return StartViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mDataList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is StartViewHolder -> {
                val bean = mDataList[position]
                holder.titleIcon.setImageResource(ConstellationUtil.getDrawableIdByName(bean.name))
                holder.name.text = bean.name
                holder.time.text = bean.time
                if (bean.select) {
                    holder.selectBg.setBackgroundResource(R.mipmap.item_compatibility_bg)
                } else {
                    holder.selectBg.setBackgroundResource(R.drawable.shape_compatiblity_bg_false)
                }
                holder.itemView.setOnClickListener {
                    mOnItemClickedListener!!.onItemClick(it, position,"")
                    notifyDataSetChanged()
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return TYPE_START
    }

    companion object {
        const val TYPE_START = 0x12
    }

    inner class StartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleIcon: ImageView = itemView.findViewById(R.id.iv_item_title_icon)
        val name: TypeFaceTextView = itemView.findViewById(R.id.text_view_name)
        val time: TypeFaceTextView = itemView.findViewById(R.id.text_view_time)
        val selectBg: LinearLayout = itemView.findViewById(R.id.linear_select_bg)

    }

}